package com.resataurant.Controller.User;

import com.resataurant.Entity.Dish;
import com.resataurant.Service.Impl.UserServiceImpl;
import com.resataurant.Service.UserService;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;
@WebServlet(name = "UserSearchServlet",urlPatterns = "/UserSearchServlet")
public class UserSearchServlet extends HttpServlet {

    UserService user=new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");//防止中文乱码

        String DishName=request.getParameter("DishName");//获取用户账号
        System.out.println("搜索菜品"+DishName);
        List<Dish> list=null;
        if(DishName==null || DishName.isEmpty()){
            list=user.searchDish();
        }
        else{
            list=user.searchDish(DishName);
        }
        String res="1";
        request.getSession().setAttribute("list",list);
        request.getSession().setAttribute("res",res);
        System.out.println(request.getSession().getAttribute("res"));
        for (Dish dish : list) {
            System.out.println(dish.getDishName());
            System.out.println(dish.getDishImage());
        }
        response.sendRedirect("user.jsp");
    }
}
